import request from '@/utils/request'
import baseUrl from "@/api/baseUrl";

export function user(email, password) { //登录接口
    return request({ //使用封装好的 axios 进行网络请求
        url: baseUrl + "login?username=" + email + "&password=" + password,
        method: 'post',
        // data: {
        //     email,
        //     password
        // }
    })
}

export function register(email, password, verify) { //注册接口
    return request({ //使用封装好的 axios 进行网络请求
        url: baseUrl + "api/register/",
        method: 'post',
        data: {
            email,
            password,
            verify
        }
    })
}

export function verify(email) { //请求验证码
    return request({ //使用封装好的 axios 进行网络请求
        url: baseUrl + "api/register/sendVertiry?email=" + email,
        method: 'post',
        // data: {
        //     email,
        // }
    })
}

export function tokenVerify(token) {
    return new Promise((resolve, reject) => {
        request({
            url: baseUrl + 'info',
            method: 'post',
            headers: {
                Authorization: token
            }
        }).then(response => {
            resolve(response)
        }).catch(error => {
            reject(error)
        })
    })
}

export function getUserInfo(token) {
    return new Promise((resolve, reject) => {
        request({
            url: baseUrl + "info",
            method: 'get',
            headers: {
                Authorization: token
            }
        }).then(response => {
            resolve(response)
        }).catch(error => {
            reject(error)
        })
    })
}

export function getUserInfoById(token, id) {
    return new Promise((resolve, reject) => {
        request({
            url: baseUrl + "info/id?id=" + id,
            method: 'get',
            headers: {
                Authorization: token,
            }
        }).then(response => {
            resolve(response)
        }).catch(error => {
            reject(error)
        })
    })
}

export function getHistoryAvatar(token) {
    return new Promise((resolve, reject) => { //封装一个 Promise
        request({
            url: baseUrl + "info/historyAvatar",
            method: 'get',
            headers: {
                Authorization: token
            }
        }).then(response => { //使用 user 接口进行网络请求
            resolve(response) //将结果封装进 Promise
        }).catch(error => {
            reject(error)
        })
    })
}

export function deleteHistoryAvatar(token, avatarHistoryId) {
    return request({
        url: baseUrl + "info/deleteHistoryAvatar",
        method: 'get',
        headers: {
            Authorization: token,
            avatarHistoryId: avatarHistoryId
        }
    })
}

export function updateUserInfo(token, userInfo) {
    return new Promise((resolve, reject) => {
        request({
            url: baseUrl + "info",
            method: 'put',
            headers: {
                Authorization: token,
                // avatarHistoryId: avatarHistoryId
            },
            data:{
                userInfo: userInfo
            }
        }).then(response => {
            resolve(response)
        }).catch(error => {
            reject(error)
        })
    })
}

export function useHistoryAvatar(token, avatarHistoryId) {
    return new Promise((resolve, reject) => {
        request({
            url: baseUrl + "info/updateUserAvatar?avatarHistoryId=" + avatarHistoryId,
            method: 'post',
            headers: {
                Authorization: token,
                // avatarHistoryId: avatarHistoryId
            }
        }).then(response => {
            resolve(response)
        }).catch(error => {
            reject(error)
        })
    })
}

export function addComment(token, gameId, msg, replyTo) {
    return new Promise((resolve, reject) => {
        request({
            url: baseUrl + "api/gameOperate/addComment?gameId=" + gameId + '&msg=' + msg + '&replyTo=' + replyTo,
            method: 'post',
            headers: {
                Authorization: token,
                // gameId: gameId,
            },
            data: {
                msg: "\"" + msg + "\""
            }
        }).then(response => {
            resolve(response)
        }).catch(error => {
            reject(error)
        })
    })
}

export function deleteComment(token, commentId) {
    return request({
        url: baseUrl + "api/game/deleteComment",
        method: 'post',
        headers: {
            Authorization: token,
            commentId: commentId,
        }
    })
}

export function addMark(token, gameId) {
    return request({
        url: baseUrl + "api/game/addMark",
        method: 'post',
        headers: {
            Authorization: token,
            avatarHistoryId: gameId,
        }
    })
}

export function cancelMark(token, gameId) {
    return request({
        url: baseUrl + "api/game/cancelMark",
        method: 'post',
        headers: {
            Authorization: token,
            avatarHistoryId: gameId,
        }
    })
}

export function addScores(token, gameId, gameScores) {
    return new Promise((resolve, reject) => {
        request({
            url: baseUrl + "api/gameOperate/addScores?gameId=" + gameId + '&gameScores=' + gameScores,
            method: 'post',
            headers: {
                Authorization: token,
            },
            // data: {
            //     gameScores: gameScores,
            // }
        }).then(response => {
            resolve(response)
        }).catch(error => {
            reject(error)
        })
    })
}

export function selectBought(token, pageNum, pageSize) {
    return new Promise((resolve, reject) => { //封装一个 Promise
        request({
            url: baseUrl + "api/game/selectBought?pageNum=" + pageNum + '&pageSize=' + pageSize,
            method: 'get',
            headers: {
                Authorization: token,
            }
        }).then(response => { //使用 user 接口进行网络请求
            resolve(response) //将结果封装进 Promise
        }).catch(error => {
            reject(error)
        })
    })
}

export function selectComment(token, gameId, pageNum, pageSize) {
    return request({
        url: baseUrl + "api/game/SelectComment",
        method: 'get',
        headers: {
            Authorization: token,
            gameId: gameId,
            pageNum: pageNum,
            pageSize: pageSize
        }
    })
}

export function selectCommented(token, pageNum, pageSize) {
    return request({
        url: baseUrl + "api/game/selectCommented",
        method: 'get',
        headers: {
            Authorization: token,
            pageNum: pageNum,
            pageSize: pageSize
        }
    })
}

export function selectMarkedGame(token, pageNum, pageSize) {
    return request({
        url: baseUrl + "api/game/selectMarkedGame",
        method: 'get',
        headers: {
            Authorization: token,
            pageNum: pageNum,
            pageSize: pageSize
        }
    })
}

export function selectScoredGame(token, pageNum, pageSize) {
    return request({
        url: baseUrl + "api/game/selectScoredGame",
        method: 'get',
        headers: {
            Authorization: token,
            pageNum: pageNum,
            pageSize: pageSize
        }
    })
}

export function checkIsBought(token, gameId) {
    return new Promise((resolve, reject) => { //封装一个 Promise
        request({
            url: baseUrl + "api/gameOperate/checkIsBought?gameId=" + gameId,
            method: 'get',
            headers: {
                Authorization: token,
            }
        }).then(response => { //使用 user 接口进行网络请求
            resolve(response) //将结果封装进 Promise
        }).catch(error => {
            reject(error)
        })
    })
}
